<template>
	<view v-if="goods_info.goods_price" class="good_detail_wais">
		<swiper class="swiper_goods_list" indicator-dots autoplay interval="3000" duration="500" circular>
			<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
				<image :src="item.pics_big" @click="privew(index)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品详情区域 -->
		<view class="goods_detail_wai">

			<view class="goods_detail_top">
				{{goods_info.goods_price|tofix}}
			</view>
			<view class="goods_detail_center">
				<view class="goods_detail_center_left">
					{{goods_info.goods_name}}
				</view>
				<view class="goods_detail_center_right">
					<view class="goods_detail_center_top">
						<uni-icons type="star" size="18" color="gray"></uni-icons>
					</view>
					<view class="goods_detail_center_buttom">
						收藏
					</view>
				</view>
			</view>
			<view class="goods_detail_buttom">

				运费.包邮{{showTolast}}
			</view>

		</view>
		<!-- rech-text 富文本-->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!-- 商品导航区域 -->
		<view class="goods-carts">
			<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>

</template>

<script>
	import  {mapState} from "vuex"
	import {mapMutations,mapGetters} from "vuex"
	export default {
		computed:{
			...mapState("m_cart",["cart"]),
			...mapGetters("m_cart",["total"]),
			showTolast(){
				this.a();
				return  this.cart.length;
			}
		},
		watch:{
			total:{
				handler(newValue){
					const option = this.options.find((item)=>item.text === '购物车');
						if(option){
							option.info = newValue;
						}
				},
				//首次加载完成之后立即调用
				immediate:true
			}
			
			// total(newValue){
			// 	const option = this.options.find((item)=>item.text === '购物车');
				
			// 	if(option){
			// 		option.info = newValue;
			// 	}
			// }
		},
		data() {
			return {
				goods_info: {},
				options: [{
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				],
			}

		},
		methods: {
			
			...mapMutations("m_cart",["addToCart"]),
			a(){
					console.log("a");
			},
			async getGoodsDetai(goods_id) {
				const {
					data: res
				} = await uni.$http.get(
					"/api/public/v1/goods/detail", {
						goods_id
					},
				);
				if (res.meta.status != 200) return uni.$showMsg("获取数据失败");
				// 使用正则替换字符串 正则开始 // ,并且重新赋值给这个属性名里面
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					"<img style='display:block;'").replace(/webp/g, "jpg");
				this.goods_info = res.message;

			},
			privew(index) {
				uni.previewImage({
					current: index,
					urls: this.goods_info.pics.map((item) => item.pics_big),
					indicator: "default",
					loop: true,
				});
			},
			onClick(e){
				if(e.content.text != '购物车') return 
				uni.switchTab({
					url:"../../pages/cart/cart"
				})
			},
			buttonClick(e){
				if(e.content.text!= "加入购物车") return
				//组织商品信息对象 
				//在调用addToCart方法
				//{ goods_ id, goods_ name, goods_ price, goods_ count, goods_ small_ Logo, goods_ state}				console.log("这是cart",this.cart);
				const goods = {
					goods_id:this.goods_info.goods_id, 
					goods_name:this.goods_info.goods_name, 
					goods_price:this.goods_info.goods_price, 
					goods_count:1, 
					goods_small_Logo:this.goods_info.goods_small_logo, 
					goods_state:true
				}
				this.addToCart(goods);
			}
			

		},
		onLoad(options) {
			const goods_id = options.goods_id;
			console.log(goods_id);
			this.getGoodsDetai(goods_id);
		},
		filters: {
			tofix(num) {
				var Num = Number(num).toFixed(2);
				return "￥" + Num;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.swiper_goods_list {
		width: 100%;
		height: 750rpx;
	}

	.swiper_goods_list swiper-item {
		width: 100%;
		height: 100%;
	}

	.swiper_goods_list image {
		width: 100%;
		height: 100%;
	}

	.goods_detail_center {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.goods_detail_center_right {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.goods_detail_wai {
		padding: 0 10px;
	}

	.goods_detail_top {
		color: #c00000;
		font-size: 18px;
		margin: 10px 0;
		text-align: left;
	}

	.goods_detail_center_left {
		font-size: 13px;
		margin-right: 10px;
		height: 50px;
		overflow: hidden;
	}

	.goods_detail_center_right {
		width: 140px;
		font-size: 12px;
		height: 100%;
		height: 50px;
		border-left: 1px solid #efefef;
	}

	.goods_detail_buttom {
		margin-top: 0rpx;
		margin-bottom: 10rpx;
		font-size: 12px;
		color: gray;
	}

	.goods_detail_center_buttom {
		color: gray;
		font-size: 13px;
	}
	.goods-carts{
		width: 100%;
		position: fixed;
		bottom: 0;
	}
	.good_detail_wais{
		padding-bottom: 50rpx;
	}
</style>
